package www.nupter.org.njuptask.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.LongSparseArray;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import org.xutils.http.RequestParams;

import www.nupter.org.njuptask.R;
import www.nupter.org.njuptask.http.HttpCallBack;
import www.nupter.org.njuptask.http.HttpRequestUtil;
import www.nupter.org.njuptask.http.RequestUrl;
import www.nupter.org.njuptask.model.BaseInfo;
import www.nupter.org.njuptask.utils.DataUtils;


/**
 * A login screen that offers login via email/password.
 */
public class RegisterActivity extends BaseActivity {

    private EditText stuNumEdit;
    private EditText mPasswordView;
    private EditText mNameEdit;
    private View mLogin;
    private RadioGroup radioGroup;
    private ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("正在注册，请等待");
        stuNumEdit = (EditText) findViewById(R.id.stuNum);
        mPasswordView = (EditText) findViewById(R.id.password);
        mNameEdit=(EditText)findViewById(R.id.name);
        mLogin = findViewById(R.id.login_button);
        radioGroup=(RadioGroup)findViewById(R.id.radio);
        setTitle("注册");
        RadioButton radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
        final String type=radioButton.getText().toString();
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressDialog.show();
                RequestParams requestParams = new RequestParams(RequestUrl.register);
                requestParams.addBodyParameter("name",mNameEdit.getText().toString());
                requestParams.addBodyParameter("account",stuNumEdit.getText().toString());
                requestParams.addBodyParameter("pwd",mPasswordView.getText().toString());
                if (type.equals("我是学生")){
                    requestParams.addBodyParameter("type","0");
                }else{
                    requestParams.addBodyParameter("type","1");
                }
                HttpRequestUtil.postMethod(requestParams, BaseInfo.class, new HttpCallBack<BaseInfo>() {
                    @Override
                    public void success(BaseInfo data) {
                        progressDialog.dismiss();
                        DataUtils.putPreferences("userId",data.getData());
                        Intent intent=new Intent(RegisterActivity.this,QuestionListActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void erro(String res) {
                        BaseToast(res);
                    }
                });

            }
        });

    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        if (component == TitleBar.LEFT) {
            Intent intent = new Intent();
            intent.setClass(RegisterActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }
}

